unit uFrmArrumaXMLNFCe;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, JvExMask, JvToolEdit, ExtCtrls, DB, MemDS, DBAccess,
  Uni, Grids, DBGrids, DBCtrls, Buttons, xmldom, XMLIntf, msxmldom, XMLDoc;

type
  TFrmArrumaXMLNFCe = class(TForm)
    uqNFCe: TUniQuery;
    PNLPesquisa: TPanel;
    JvDateEditInicial: TJvDateEdit;
    JvDateEditFinal: TJvDateEdit;
    EdtStatus: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BtnPesquisar: TButton;
    PNLReparos: TPanel;
    DBGrid1: TDBGrid;
    dsNotas: TUniDataSource;
    uqNFCeIDMOV: TIntegerField;
    uqNFCeNUMEROMOV: TStringField;
    uqNFCeCSTAT: TStringField;
    uqNFCeCHAVEACESSO: TStringField;
    uqNFCeDATAEMISSAO: TDateField;
    uqNFCeDATASAIDA: TDateField;
    uqNFCeXMLNFE: TMemoField;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    dbeNumero: TDBEdit;
    dbeChaveAntiga: TDBEdit;
    dbeDtAntiga: TDBEdit;
    uqNFCeCODTMV: TStringField;
    JvDateEditCerta: TJvDateEdit;
    EdtChaveCerta: TEdit;
    BtnReparar: TBitBtn;
    XMLDocument: TXMLDocument;
    uqNFCeAlt: TUniQuery;
    edtRegistro: TEdit;
    Label9: TLabel;
    procedure BtnPesquisarClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BtnRepararClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    vNode : IXMLNode;
    vRoot : IXMLNode;
  end;

var
  FrmArrumaXMLNFCe: TFrmArrumaXMLNFCe;

implementation

uses uDM;

{$R *.dfm}

procedure TFrmArrumaXMLNFCe.BtnPesquisarClick(Sender: TObject);
begin
  if EdtStatus.Text = '' then
    Exit;

  uqNFCe.Close;
  uqNFCe.FetchRows := StrToInt(edtRegistro.Text);
  uqNFCe.ParamByName('dtinicial').AsDate := JvDateEditInicial.Date;
  uqNFCe.ParamByName('dtfinal').AsDate := JvDateEditFinal.Date;
  uqNFCe.ParamByName('cstat').AsString := EdtStatus.Text;
  //uqNFCe.ParamByName('qtde').AsInteger := StrToInt(edtRegistro.Text);
  uqNFCe.Open;

end;

procedure TFrmArrumaXMLNFCe.BtnRepararClick(Sender: TObject);
var i1 : Integer;
  i2 : Integer;
  vChaveCerta : string;
  vStringStream : TStringStream;
  vDataCerta : TDate;
begin
  vStringStream := nil;
  vDataCerta := JvDateEditCerta.Date;

  try
    vStringStream := TStringStream.Create;
    vStringStream.WriteString(uqNFCeXMLNFE.Value);
    //ShowMessage(vStringStream.DataString);
    //ShowMessage(vStringStream.DataString);
    XMLDocument.LoadFromStream(vStringStream);
    vChaveCerta := EdtChaveCerta.Text;
    vNode := XMLDocument.ChildNodes.FindNode('nfeProc');
    if Assigned(vNode) then
    begin
      //repara infnfe
      XMLDocument.ChildNodes.FindNode('nfeProc').ChildNodes.FindNode('NFe').ChildNodes.FindNode('infNFe').Attributes['Id'] := 'NFe' + vChaveCerta;
      //signature
      XMLDocument.ChildNodes.FindNode('nfeProc').ChildNodes.FindNode('NFe').ChildNodes[1].ChildNodes[0].ChildNodes[2].Attributes['URI'] := '#NFe' + vChaveCerta;
      //protNfe chNfe
      XMLDocument.ChildNodes.FindNode('nfeProc').ChildNodes.FindNode('protNFe').ChildNodes.FindNode('infProt').ChildNodes.FindNode('chNFe').NodeValue := vChaveCerta;
    end
    else
    begin
      //repara infnfe
      XMLDocument.ChildNodes.FindNode('NFe').ChildNodes.FindNode('infNFe').Attributes['Id'] := 'NFe' + vChaveCerta;
      //signature
      XMLDocument.ChildNodes.FindNode('NFe').ChildNodes[1].ChildNodes[0].ChildNodes[2].Attributes['URI'] := '#NFe' + vChaveCerta;
      //protNfe chNfe
      //XMLDocument.ChildNodes.FindNode('protNFe').ChildNodes.FindNode('infProt').ChildNodes.FindNode('chNFe').NodeValue := vChaveCerta;
    end;



    vStringStream.Clear;
    XMLDocument.SaveToStream(vStringStream);
    //ShowMessage(vStringStream.DataString);
    with uqNFCeAlt do
    begin
      Close;
      SQL.Text := 'select * from tnfe where idmov = ' + uqNFCeIDMOV.AsString;
      Open;
      Edit;
      FieldByName('XMLNFE').AsString := vStringStream.DataString;
      FieldByName('CHAVEACESSO').Value := vChaveCerta;
      Post;
      Close;
      SQL.Text := 'select * from tmov where idmov = ' + uqNFCeIDMOV.AsString;
      Open;
      Edit;
      FieldByName('DATAEMISSAO').AsDateTime := vDataCerta;
      FieldByName('DATASAIDA').AsDateTime := vDataCerta;
      Post;
    end;
    uqNFCe.Refresh;

  finally
    vStringStream.Free;
  end;






end;

procedure TFrmArrumaXMLNFCe.FormShow(Sender: TObject);
begin
  JvDateEditInicial.Date := Date;
  JvDateEditFinal.Date := Date;
end;

end.
